package com.example.teacher.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commonutils.ResultData;
import com.example.commonutils.constants.MqConstants;
import com.example.teacher.entity.EduCourse;
import com.example.teacher.entity.Teacher;
import com.example.teacher.entity.course.CourseInfo;
import com.example.teacher.entity.vo.CourseInfoVo;
import com.example.teacher.entity.vo.CoursePublishVo;
import com.example.teacher.entity.vo.CourseQuery;
import com.example.teacher.service.EduCourseService;
import com.example.teacher.service.TeacherService;
import com.example.teacher.utils.CourseStatus;
import io.netty.util.internal.StringUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-05-25
 */
@RestController
@RequestMapping("/backstage/course")
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

//    private Integer courseCount=0;

    //购买数增加的接口
    @GetMapping(value = "buycount/{courseId}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void buyCount(@PathVariable String courseId){
        EduCourse course = courseService.getById(courseId);
        Long buyCount = course.getBuyCount();
        EduCourse course1=new EduCourse();
        course1.setId(courseId);
        course1.setBuyCount(buyCount+1);
        courseService.updateById(course1);
    }

    @GetMapping("getCourseCount")
    public Integer CourseCount(){
        String courseCount = redisTemplate.opsForValue().get("courseCount");
        //统计之后将数据初始化
        redisTemplate.opsForValue().set("courseCount","0");
        return Integer.parseInt(courseCount);
    }

    //新增课程
    @PostMapping(value = "addCourseInfo",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回添加的课程id，以便大纲页面查询使用
        String id = courseService.saveCourseInfo(courseInfoVo);
        return ResultData.ok().data("courseId",id);
    }
    //根据课程id查询
    @GetMapping(value = "getCourseInfo/{courseId}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo=courseService.getCourseInfo(courseId);
        return ResultData.ok().data("courseInfoVo",courseInfoVo);
    }
    @GetMapping(value = "getCourseById/{courseId}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public EduCourse getCourseById(@PathVariable String courseId){
        return courseService.getById(courseId);
    }
    //修改课程信息
    @PostMapping(value = "updateCourseInfo",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        rabbitTemplate.convertAndSend(MqConstants.LEARN_EXCHANGE,MqConstants.LEARN_INSERT_KEY,courseInfoVo.getId());
        return ResultData.ok();
    }
    //根据课程id查询用户填写的前两步信息
    @GetMapping(value = "getPublishInfo/{courseId}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData getPublishInfo(@PathVariable String courseId){
        CoursePublishVo publishCourseInfo = courseService.publishCourseInfo(courseId);
        return ResultData.ok().data("publishCourse",publishCourseInfo);
    }

    @PutMapping(value = "publishStatus/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData publishStatus(@PathVariable String id){
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(id);
        //改变状态为已发布
        redisTemplate.opsForValue().increment("courseCount",1l);
        eduCourse.setStatus(CourseStatus.STATUS_NORMAL);
        courseService.updateById(eduCourse);
        rabbitTemplate.convertAndSend(MqConstants.LEARN_EXCHANGE,MqConstants.LEARN_INSERT_KEY,id);
        return ResultData.ok();
    }

    //条件查询待分页
    @PostMapping(value = "getCourseCondition/{page}/{limit}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData getCourseCondition(
            @PathVariable("page") Long page,
            @PathVariable("limit") Long limit,
            @RequestBody(required = false) CourseQuery courseQuery
            ){
        Page<EduCourse> pageCourse = new Page<>(page,limit);
        return courseService.getCourseCondition(pageCourse,courseQuery);
    }

    @DeleteMapping(value = "deleteCourse/{courseId}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData deleteCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        rabbitTemplate.convertAndSend(MqConstants.LEARN_EXCHANGE,MqConstants.LEARN_DELETE_KEY,courseId);
        return ResultData.ok();
    }
}

